import pagesJson from '../pages.json';

/**
 * 创建路由对象
 * @param {*} pages
 * @param {*} rootPath
 * @returns
 */
const createRoutes = (pages, rootPath = '/') => {
    const routes = [];
    pages.map(page => {
        const route = {
            path: rootPath + page.path,
            name: page.name || '',
            requireAuth: page.requireAuth
        };
        routes.push(route);
    });
    return routes;
};

/**
 * 收集路由地址
 * @returns
 */
const main = () => {
    const mainPack = createRoutes(pagesJson.pages);
    const routes = [...mainPack];
    pagesJson.subpackages &&
        pagesJson.subpackages.forEach(subpack => {
            const subpackRoutes = createRoutes(subpack.pages, '/' + subpack.root + '/');
            routes.push(...subpackRoutes);
        });
    return routes;
};

export default main();
